Merge pull request #23 from cantino/check_key

Add some sanity checks to the WeatherAgent

Andrew Cantino 12 年之前
父节点
当前提交
9c827a5e0c
共有 1 个文件被更改,包括 12 次插入6 次删除
  1. 12 6
      app/models/agents/weather_agent.rb

+ 12 - 6
app/models/agents/weather_agent.rb

@@ -44,12 +44,16 @@ module Agents
44 44
     end
45 45
 
46 46
     def wunderground
47
-      Wunderground.new(options[:api_key])
47
+      Wunderground.new(options[:api_key]) if key_setup?
48
+    end
49
+
50
+    def key_setup?
51
+      options[:api_key] && options[:api_key] != "your-key"
48 52
     end
49 53
 
50 54
     def default_options
51 55
       {
52
-        :api_key => "",
56
+        :api_key => "your-key",
53 57
         :zipcode => "94103"
54 58
       }
55 59
 
@@ -57,13 +61,15 @@ module Agents
57 61
 
58 62
     def validate_options
59 63
       errors.add(:base, "zipcode is required") unless options[:zipcode].present?
60
-      errors.add(:base, "api_key is required") unless options[:api_key].present?
64
+      errors.add(:base, "api_key is required") unless key_setup?
61 65
     end
62 66
 
63 67
     def check
64
-      wunderground.forecast_for(options[:zipcode])["forecast"]["simpleforecast"]["forecastday"].each do |day|
65
-        if is_tomorrow?(day)
66
-          create_event :payload => day.merge(:zipcode => options[:zipcode])
68
+      if key_setup?
69
+        wunderground.forecast_for(options[:zipcode])["forecast"]["simpleforecast"]["forecastday"].each do |day|
70
+          if is_tomorrow?(day)
71
+            create_event :payload => day.merge(:zipcode => options[:zipcode])
72
+          end
67 73
         end
68 74
       end
69 75
     end